package org.study.shardingsphere.ds.config;

import org.apache.ibatis.type.*;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;

/**
 * 解决 ShardingSphere ResultSet 转换 LocalDateTime 的问题
 *
 * @author lenovo
 */
@Component
@MappedTypes(LocalDateTime.class)
@MappedJdbcTypes(value = JdbcType.DATE, includeNullJdbcType = true)
public class MyLocalDateTimeTypeHandler extends LocalDateTimeTypeHandler {
    @Override
    public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
        Object object = rs.getObject(columnName);
        if (object instanceof LocalDateTime) {
            return (LocalDateTime) object;
        }
        return null;
    }
}
